//
//  IDGenerator.m
//  MoCard
//
//  Created by 水石 温 on 11-9-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "IDGenerator.h"

char cs[]="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";


@implementation IDGenerator
@synthesize /*defaultGenerator,*/idPrefix;

- (id)init
{
    self = [super init];
    idPrefix=nil;
    idSuffix=0;
    return self;
}

+(IDGenerator*)share {
    static id instance=nil;
    if (instance==nil) {
        instance=[IDGenerator new];
    }
    return instance;
}

-(NSString*)genID {
    if (idSuffix>99 || idPrefix==nil) {
        [self generateNewPrefix];
        idSuffix=0;
    }
    return [NSString stringWithFormat:@"%@-%02d",idPrefix,idSuffix++];
}


-(void)generateNewPrefix {
    char c1[ID_LEN-2];
    
    //srand((unsigned)time(0));
    memset(c1, 0, ID_LEN-2);
    for (int i=0; i<ID_LEN-3; i++) {
        c1[i]=cs[arc4random()%62];
        //printf("%c\n",c1[i][j]);
    }
    self.idPrefix=[NSString stringWithUTF8String:c1];
    //free(c1);
}


-(void)dealloc {
}

@end
